package stu.sup.controller.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import stu.sup.entity.AddressInfo;
import stu.sup.service.IAddressInfoService;
import stu.sup.util.JsonResult;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 本类用来演示：
 *
 * @author:Administrator
 * @data:2020/7/5 0005
 */
@Controller
public class AddressInfoController {

    @Autowired
    private IAddressInfoService addressInfoService;


    @RequestMapping("/add-address")
    public String addAdd(){
        return "/view/add-address";
    }

    /**
     * 添加收货地址
     * @param name
     * @param address
     * @param telephone
     * @param request
     * @return
     */
    @PostMapping("/view/add-address")
    @ResponseBody
    public JsonResult addAddress(String name, String address, String telephone, HttpServletRequest request){
        System.out.println("输入的收件人:"+name);
        System.out.println("地址:"+address);
        System.out.println("电话:"+telephone);

        AddressInfo addressInfo = new AddressInfo();
        addressInfo.setUserLoginId((Integer) request.getSession().getAttribute("loginId"));
        addressInfo.setName(name);
        addressInfo.setAddress(address);
        addressInfo.setTelephone(telephone);

        boolean ad = addressInfoService.addAdress((Integer) request.getSession().getAttribute("loginId"),name,address,telephone);


        if (ad){
            return new JsonResult(1,"添加成功",addressInfo);
        }else {
            return new JsonResult(0,"添加失败",addressInfo);
        }
    }

    /**
     * 查询登录用户的收货地址
     */
    @RequestMapping("/address")
    public String addressInfo(Model model, HttpServletRequest request){
        //查询登录账号下的所有地址
         List<AddressInfo> addressInfoList =  addressInfoService.selectByLoginId((Integer) request.getSession().getAttribute("loginId"));
         model.addAttribute("addressInfo",addressInfoList);
         System.out.println("addressInfoList:"+addressInfoList);
         return "/view/address";
    }

    /**
     * 删除地址
     */
    @DeleteMapping("/address/delete")
    @ResponseBody
    public String delAddress(Integer addId,HttpServletRequest request){
        System.out.println("addId:"+addId);

        addressInfoService.delete(addId);

        return "/address";
    }

    /**
     * 根据主键id查询收货地址信息
     */
    @PostMapping("/address/select")
    @ResponseBody
    public String selectAddress(Integer addId,HttpServletRequest request){
        System.out.println("addId:"+addId);
        AddressInfo addressInfo = new AddressInfo();
        addressInfo = addressInfoService.selectByPrimaryKey(addId);
        request.getSession().setAttribute("address",addressInfo);
        request.getSession().setAttribute("addressInfoId",addressInfo.getAddressInfoId());
        return null;
    }

    /**
     * 修改收货地址信息
     */
    @PostMapping("/view/update")
    @ResponseBody
    public JsonResult updateAdddress(@RequestParam("name") String name,@RequestParam("address") String address,@RequestParam("telephone") String telephone, HttpServletRequest request){
        System.out.println("输入的收件人:"+name);
        System.out.println("地址:"+address);
        System.out.println("电话:"+telephone);

        AddressInfo addressInfo = new AddressInfo();
        addressInfo.setAddressInfoId((Integer) request.getSession().getAttribute("addressInfoId"));
        addressInfo.setUserLoginId((Integer) request.getSession().getAttribute("loginId"));
        addressInfo.setName(name);
        addressInfo.setAddress(address);
        addressInfo.setTelephone(telephone);
        boolean updateAdd = addressInfoService.update(addressInfo);

        if (updateAdd){
            return new JsonResult(1,"修改成功",addressInfo);
        }else {
            return new JsonResult(0,"修改失败",addressInfo);
        }
    }

}
